﻿// Developer: Cory Fowler
// Company: Innosphere SDG

namespace WebRole1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using WebRole1.LittleBlackBook;
    using WebRole1.LittleBlackBook.Data;

    public partial class People : System.Web.UI.Page
    {
        LittleBlackBookEntities lbbContext;
        Uri svrUrl = new Uri("http://littleblackbookdb.cloudapp.net/LittleBlackBookService.svc/");

        protected void Page_Load(object sender, EventArgs e)
        {
            lbbContext = new LittleBlackBookEntities(svrUrl);
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            PeopleList.DataSource = lbbContext.people;
            PeopleList.DataBind();
        }

        protected void PeopleList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridView grid = sender as GridView;
            
            if (grid != null)
            {
                HiddenField personIdField = grid.Rows[e.RowIndex].FindControl("id") as HiddenField;

                if (personIdField != null)
                {
                    int personId = 0;
                    Int32.TryParse(personIdField.Value, out personId);

                    DeletePerson(personId);
                }
            }
            e.Cancel = true;
        }

        protected void PeopleList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridView grid = sender as GridView;

            if (grid != null)
            {
                HiddenField personIdField = grid.Rows[e.NewSelectedIndex].FindControl("id") as HiddenField;

                if (personIdField != null)
                {
                    int personId = 0;
                    Int32.TryParse(personIdField.Value, out personId);

                    FillPersonForEdit(personId);
                }
            }
            e.Cancel = true;
        }

        private void DeletePerson(int personId)
        {
            try
            {
                people person = GetPerson(personId);

                lbbContext.DeleteObject(person);
                lbbContext.SaveChanges();
            }
            catch
            {
                // Inform user of Error
            }
        }

        private void FillPersonForEdit(int personId)
        {
            people person = GetPerson(personId);

            id.Value = personId.ToString();
            fname.Text = person.fname;
            lname.Text = person.lname;
            phonenumber.Text = person.phonenumber;
        }

        private people GetPerson(int personId)
        {
            try
            {
                return (from p in lbbContext.people
                        where p.ID == personId
                        select p).FirstOrDefault();
            }
            catch
            {
                return null;
            }
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                int personId = 0;
                Int32.TryParse(id.Value, out personId);

                SavePerson(personId);
                PeopleList.DataBind();
                ClearForm();
            }
        }

        private void SavePerson(int personId)
        {
            people person = GetPerson(personId) ?? new people();

            person.fname = HttpUtility.HtmlEncode(fname.Text);
            person.lname = HttpUtility.HtmlEncode(lname.Text);
            person.phonenumber = HttpUtility.HtmlEncode(phonenumber.Text);

            try
            {
                if (personId <= 0)
                    lbbContext.AddTopeople(person);
                else
                    lbbContext.UpdateObject(person);
                lbbContext.SaveChanges();
            }
            catch
            {
                // Inform User of Error
            }
        }

        private void ClearForm()
        {
            id.Value = string.Empty;
            fname.Text = string.Empty;
            lname.Text = string.Empty;
            phonenumber.Text = string.Empty;
        }
    }
}
